package com.smmisia.shop.entity;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.smmisia.common.enums.DeletedEnum;
import com.smmisia.shop.constant.ProductStateEnum;
import com.smmisia.shop.entity.convetor.ProductConvertor;
import com.smmisia.shop.request.ProductCreateRequest;
import lombok.Data;

/**
 * 
 * @TableName product
 */
@TableName(value ="product")
@Data
public class Product implements Serializable {
    /**
     * 主键ID（自增主键）
     */
    @TableId
    private Long id;

    /**
     * 商品名称
     */
    private String name;

    /**
     * 商品封面
     */
    private String cover;

    /**
     * 商品类目id
     */
    private String classId;

    /**
     * 商品图片
     */
    private String images;

    /**
     * 价格
     */
    private BigDecimal price;

    /**
     * 商品数量
     */
    private Long quantity;

    /**
     * 详情
     */
    private String detail;

    /**
     * 可销售库存
     */
    private Long saleableInventory;

    /**
     * 幂等号
     */
    private String identifier;

    /**
     * 已占用库存
     */
    private Long occupiedInventory;

    /**
     * 状态
     */
    private ProductStateEnum state;

    /**
     * 创建时间
     */
    private LocalDateTime createTime;

    /**
     * 发售时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime saleTime;

    /**
     * 逻辑删除，0为未删除，非0为已删除
     */
    private Integer deleted;

    /**
     * 乐观锁版本
     */
    private Integer lockVersion;

    /**
     * 创建者
     */
    private Integer creatorId;

    /**
     * 修改时间
     */
    private LocalDateTime updateTime;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        Product other = (Product) that;
        return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
            && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
            && (this.getCover() == null ? other.getCover() == null : this.getCover().equals(other.getCover()))
            && (this.getClassId() == null ? other.getClassId() == null : this.getClassId().equals(other.getClassId()))
            && (this.getImages() == null ? other.getImages() == null : this.getImages().equals(other.getImages()))
            && (this.getPrice() == null ? other.getPrice() == null : this.getPrice().equals(other.getPrice()))
            && (this.getQuantity() == null ? other.getQuantity() == null : this.getQuantity().equals(other.getQuantity()))
            && (this.getDetail() == null ? other.getDetail() == null : this.getDetail().equals(other.getDetail()))
            && (this.getSaleableInventory() == null ? other.getSaleableInventory() == null : this.getSaleableInventory().equals(other.getSaleableInventory()))
            && (this.getIdentifier() == null ? other.getIdentifier() == null : this.getIdentifier().equals(other.getIdentifier()))
            && (this.getOccupiedInventory() == null ? other.getOccupiedInventory() == null : this.getOccupiedInventory().equals(other.getOccupiedInventory()))
            && (this.getState() == null ? other.getState() == null : this.getState().equals(other.getState()))
            && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
            && (this.getSaleTime() == null ? other.getSaleTime() == null : this.getSaleTime().equals(other.getSaleTime()))
            && (this.getDeleted() == null ? other.getDeleted() == null : this.getDeleted().equals(other.getDeleted()))
            && (this.getLockVersion() == null ? other.getLockVersion() == null : this.getLockVersion().equals(other.getLockVersion()))
            && (this.getCreatorId() == null ? other.getCreatorId() == null : this.getCreatorId().equals(other.getCreatorId()))
            && (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
        result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
        result = prime * result + ((getCover() == null) ? 0 : getCover().hashCode());
        result = prime * result + ((getClassId() == null) ? 0 : getClassId().hashCode());
        result = prime * result + ((getImages() == null) ? 0 : getImages().hashCode());
        result = prime * result + ((getPrice() == null) ? 0 : getPrice().hashCode());
        result = prime * result + ((getQuantity() == null) ? 0 : getQuantity().hashCode());
        result = prime * result + ((getDetail() == null) ? 0 : getDetail().hashCode());
        result = prime * result + ((getSaleableInventory() == null) ? 0 : getSaleableInventory().hashCode());
        result = prime * result + ((getIdentifier() == null) ? 0 : getIdentifier().hashCode());
        result = prime * result + ((getOccupiedInventory() == null) ? 0 : getOccupiedInventory().hashCode());
        result = prime * result + ((getState() == null) ? 0 : getState().hashCode());
        result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
        result = prime * result + ((getSaleTime() == null) ? 0 : getSaleTime().hashCode());
        result = prime * result + ((getDeleted() == null) ? 0 : getDeleted().hashCode());
        result = prime * result + ((getLockVersion() == null) ? 0 : getLockVersion().hashCode());
        result = prime * result + ((getCreatorId() == null) ? 0 : getCreatorId().hashCode());
        result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode());
        return result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", name=").append(name);
        sb.append(", cover=").append(cover);
        sb.append(", classId=").append(classId);
        sb.append(", images=").append(images);
        sb.append(", price=").append(price);
        sb.append(", quantity=").append(quantity);
        sb.append(", detail=").append(detail);
        sb.append(", saleableInventory=").append(saleableInventory);
        sb.append(", identifier=").append(identifier);
        sb.append(", occupiedInventory=").append(occupiedInventory);
        sb.append(", state=").append(state);
        sb.append(", createTime=").append(createTime);
        sb.append(", saleTime=").append(saleTime);
        sb.append(", deleted=").append(deleted);
        sb.append(", lockVersion=").append(lockVersion);
        sb.append(", creatorId=").append(creatorId);
        sb.append(", updateTime=").append(updateTime);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }

    public static Product create(ProductCreateRequest request){
        Product product = ProductConvertor.INSTANCE.mapToEntity(request);
        product.setOccupiedInventory(0L);
        product.setSaleableInventory(request.getQuantity());
        product.setState(ProductStateEnum.SUCCEED);
        product.setDeleted(DeletedEnum.IS_UNDELETED.getCode());
        product.setCreateTime(LocalDateTime.now());
        product.setLockVersion(0);
        product.setCreatorId(Integer.valueOf((String) StpUtil.getLoginId()));
        product.setUpdateTime(LocalDateTime.now());
        return product;
    }

    public Product remove() {
        this.state = ProductStateEnum.REMOVED;
        return this;
    }
}